﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Utility;
using LotteryVote.Logic.Application;
using LotteryVote.Logic.Web;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote
{
    public partial class LimitManager : AgentBasePage
    {
        int UserId;
        CompanyTypeFacade comTypeFacade = new CompanyTypeFacade();
        BetLimitFacade blGameBetLimit = new BetLimitFacade();
        GamePlayWayFacade gpwFacade = new GamePlayWayFacade();
        public IList<BetLimit> BetLimitList { get; private set; }
        public IList<GameBetLimit> GameBetLimitList { get; private set; }
        protected IList<GamePlayWay> GamePlayWayList;
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }

        void InitParams()
        {
            Execute(() =>
            {
                UserId = int.Parse(Request["Userid"]);

                BetLimitList = blGameBetLimit.ListLimitByUser(UserId);
                if (BetLimitList == null || BetLimitList.Count == 0)
                    throw new InvalidDataException(string.Format("LimitManager,UserId:{0},CurrentUser:{1}", UserId, CurrentUser.ToString()));
                GameBetLimitList = blGameBetLimit.ListGameLimitByUser(UserId);
                GamePlayWayList = gpwFacade.GetAllGamePlayWay();
            });
        }

        void CheckParentRole()
        {
            UserFacade userFacade = new UserFacade();
            var user = userFacade.GetUser(UserId);
            if (user.ParentId != CurrentUser.UserId)
                throw new NoPermissionException("Update User Limit");
        }

        public GamePlayWay FindGamePlayWay(int gameId, int wayId)
        {
            return Execute(() =>
            {
                var gamePlayWay = LotterySystem.Current.FindGamePlayWay(gameId, wayId);
                return gamePlayWay;
            });
        }

        public BetLimit FindBetLimit(int gameId)
        {
            return Execute(() =>
            {
                return BetLimitList.Find(it => it.GameId == gameId);
            });
        }
        public GameBetLimit FindGameLimit(int companyId, int gamePlayWayId)
        {
            return Execute(() =>
            {
                return GameBetLimitList.Find(it => it.CompanyType == (CompanyType)companyId && it.GamePlayWayId == gamePlayWayId);
            });
        }
    }
}